package com.exercise.widgets.floatingbutton

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.LinearInterpolator
import android.view.animation.OvershootInterpolator
import android.widget.Toast
import androidx.core.animation.doOnEnd
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.databinding.ActivityFloatingButtonBinding
import com.exercise.widgets.floatingbutton.floatview.AvatarFloatView
import com.exercise.widgets.floatingbutton.floatview.BaseFloatView
import com.exercise.widgets.floatingbutton.floatview.FloatManager
import com.google.android.material.snackbar.Snackbar

class FloatingButtonActivity : BaseActivity<BaseViewModel, ActivityFloatingButtonBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityFloatingButtonBinding
        get() = ActivityFloatingButtonBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java
    private var mFloatView: AvatarFloatView? = null
    override fun initData() {
    }

    override fun initView() {
        binding.addFloatView.setOnClickListener {
            mFloatView = AvatarFloatView(this)
            mFloatView?.setDragDistance(0.3)
            if (binding.radioLeftAndRight.isChecked) {
                mFloatView?.setAdsorbType(BaseFloatView.ADSORB_HORIZONTAL)
            } else {
                mFloatView?.setAdsorbType(BaseFloatView.ADSORB_VERTICAL)
            }
            FloatManager.with(this).add(mFloatView!!)
                .setClickListener(object : BaseFloatView.OnFloatClickListener {
                    override fun onClick(v: View) {
                        Toast.makeText(this@FloatingButtonActivity, "Click", Toast.LENGTH_SHORT)
                            .show()
//                        Snackbar.make(v,"Click",Snackbar.LENGTH_SHORT).show()
                    }
                })
                .show()
        }
        binding.fabMain.setOnClickListener {
            if (!isShow) {
                showMenu()
            } else {
                hideMenu()
            }
        }

        binding.radioGroup.setOnCheckedChangeListener { _, checkedId ->
            if (checkedId == binding.radioLeftAndRight.id) {
                mFloatView?.setAdsorbType(BaseFloatView.ADSORB_HORIZONTAL)
            } else {
                mFloatView?.setAdsorbType(BaseFloatView.ADSORB_VERTICAL)
            }
        }

        binding.dragFloatingActionButton.setOnClickListener {
            Toast.makeText(this,"DragFloatingActionButton",Toast.LENGTH_SHORT).show()
        }

    }

    private fun hideMenu() {
        val anim1 = ObjectAnimator.ofFloat(binding.fabSub1, "translationY", 0f).apply {
            duration = 200
            interpolator = AccelerateInterpolator()
        }
        val anim2 = ObjectAnimator.ofFloat(binding.fabSub2, "translationY", 0f).apply {
            duration = 200
            interpolator = AccelerateInterpolator()
        }
        val anim3 = ObjectAnimator.ofFloat(binding.fabMain, "rotation", 0f).apply {
            duration = 200
            interpolator = LinearInterpolator()
        }
        val animatorSet = AnimatorSet().apply {
            doOnEnd {
                isShow = false
            }
        }
        animatorSet.playTogether(anim1, anim2, anim3)
        animatorSet.start()
    }


    private var isShow = false
    private fun showMenu() {
        val anim1 = ObjectAnimator.ofFloat(binding.fabSub1, "translationY", 0f, -160f).apply {
            duration = 300
            interpolator = OvershootInterpolator()
        }
        val anim2 = ObjectAnimator.ofFloat(binding.fabSub2, "translationY", 0f, -320f).apply {
            duration = 300
            interpolator = OvershootInterpolator()
        }
        val anim3 = ObjectAnimator.ofFloat(binding.fabMain, "rotation", 0f, -45f).apply {
            duration = 300
            interpolator = LinearInterpolator()
        }

        val animatorSet = AnimatorSet().apply {
            doOnEnd {
                isShow = true
            }
        }
        animatorSet.playTogether(anim1, anim2, anim3)
        animatorSet.start()
    }
}